package com.jzo2o.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.mapper.ActivityMapper;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponWriteOffService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-16
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
    private static final int MILLION = 1000000;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ICouponService couponService;

    @Resource
    private ICouponWriteOffService couponWriteOffService;

    //保存优惠券活动
    @Override
    public void saveActivity(ActivitySaveReqDTO activitySaveReqDTO) {

        activitySaveReqDTO.check();

        Activity activity = BeanUtils.toBean(activitySaveReqDTO, Activity.class);
        activity.setStatus(ActivityStatusEnum.NO_DISTRIBUTE.getStatus());
        activity.setStockNum(activitySaveReqDTO.getTotalNum());

        if(ObjectUtils.isEmpty(activitySaveReqDTO.getId())){
            boolean result = this.save(activity);
            if(!result){
                throw new DBException("新增优惠券活动错误");
            }
        } else {
            if(ObjectUtils.isEmpty(this.getById(activity.getId()))){
                throw new BadRequestException("当前要修改的优惠券活动不存在");
            }

            boolean result = this.updateById(activity);
            if(!result){
                throw new DBException("修改优惠券活动错误");
            }
        }
    }

    //运营端分页查询活动
    @Override
    public PageResult<ActivityInfoResDTO> queryPage(ActivityQueryForPageReqDTO activityQueryForPageReqDTO) {
        Page<Activity> page = new Page<>(activityQueryForPageReqDTO.getPageNo().intValue(), activityQueryForPageReqDTO.getPageSize());

        LambdaQueryWrapper<Activity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .eq(ObjectUtils.isNotEmpty(activityQueryForPageReqDTO.getId()), Activity::getId, activityQueryForPageReqDTO.getId())
                .like(StringUtils.isNotEmpty(activityQueryForPageReqDTO.getName()), Activity::getName, activityQueryForPageReqDTO.getName())
                .eq(ObjectUtils.isNotNull(activityQueryForPageReqDTO.getType()), Activity::getType, activityQueryForPageReqDTO.getType())
                .eq(ObjectUtils.isNotNull(activityQueryForPageReqDTO.getStatus()), Activity::getStatus, activityQueryForPageReqDTO.getStatus())
                .orderByDesc(Activity::getId);

        Page<Activity> activityPage = baseMapper.selectPage(page, lambdaQueryWrapper);
        PageResult<ActivityInfoResDTO> activityInfoResDTOPageResult = PageUtils.toPage(activityPage, ActivityInfoResDTO.class);

        return activityInfoResDTOPageResult;
    }

    //查询优惠券活动详情
    @Override
    public ActivityInfoResDTO queryById(Long id) {

        Activity activity = this.getById(id);

        ActivityInfoResDTO result = BeanUtils.toBean(activity, ActivityInfoResDTO.class);

        return result;
    }

    //撤销活动
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long id) {
        boolean result = this.lambdaUpdate()
                .set(Activity::getStatus, ActivityStatusEnum.VOIDED.getStatus())
                .eq(Activity::getId, id)
                .in(Activity::getStatus, Arrays.asList(ActivityStatusEnum.NO_DISTRIBUTE.getStatus(), ActivityStatusEnum.DISTRIBUTING.getStatus()))
                .update();

        if(!result){
            throw new DBException("撤销活动失败");
        }

        couponService.revoke(id);
    }
}
